home *** CD-ROM | disk | FTP | other *** search
/ LG Super CD / LG Super CD.iso / hack / azureus_2.5.0.4a_win32.setup.exe / plugins / azplugins / azplugins_2.1.4.jar / HTML / Tmpl / Util.java < prev   
Encoding:
Java Source  |  2007-01-26  |  3.1 KB  |  131 lines

  1. /*
  2. *      HTML.Template:  A module for using HTML Templates with java
  3. *
  4. *      Copyright (c) 2002 Philip S Tellis (philip.tellis@iname.com)
  5. *
  6. *      This module is free software; you can redistribute it
  7. *      and/or modify it under the terms of either:
  8. *
  9. *      a) the GNU General Public License as published by the Free
  10. *      Software Foundation; either version 1, or (at your option)
  11. *      any later version, or
  12. *
  13. *      b) the "Artistic License" which comes with this module.
  14. *
  15. *      This program is distributed in the hope that it will be
  16. *      useful, but WITHOUT ANY WARRANTY; without even the implied
  17. *      warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  18. *      PURPOSE.  See either the GNU General Public License or the
  19. *      Artistic License for more details.
  20. *
  21. *      You should have received a copy of the Artistic License
  22. *      with this module, in the file ARTISTIC.  If not, I'll be
  23. *      glad to provide one.
  24. *
  25. *      You should have received a copy of the GNU General Public
  26. *      License along with this program; if not, write to the Free
  27. *      Software Foundation, Inc., 59 Temple Place, Suite 330,
  28. *      Boston, MA 02111-1307 USA
  29. */
  30.  
  31.  
  32. package HTML.Tmpl;
  33.  
  34. public class Util
  35. {
  36.     public static boolean debug=false;
  37.  
  38.     public static String escapeHTML(String element)
  39.     {
  40.         String s = new String(element);    // don't change the original
  41.         String [] metas = {"&", "<", ">", "\""};
  42.         String [] repls = {"&", "<", ">", """};
  43.         for(int i = 0; i < metas.length; i++) {
  44.             int pos=0;
  45.             do {
  46.                 pos = s.indexOf(metas[i], pos);
  47.                 if(pos<0)
  48.                     break;
  49.  
  50.                 s = s.substring(0, pos) + repls[i] + s.substring(pos+1);
  51.                 pos++;
  52.             } while(pos >= 0);
  53.         }
  54.  
  55.         return s;
  56.     }
  57.  
  58.     public static String escapeURL(String url)
  59.     {
  60.         StringBuffer s = new StringBuffer();
  61.         String no_escape = "./-_";
  62.  
  63.         for(int i=0; i<url.length(); i++)
  64.         {
  65.             char c = url.charAt(i);
  66.             if(!Character.isLetterOrDigit(c) &&
  67.                     no_escape.indexOf(c)<0) 
  68.             {
  69.                 String h = Integer.toHexString((int)c);
  70.                 s.append("%");
  71.                 if(h.length()<2)
  72.                     s.append("0");
  73.                 s.append(h);
  74.             } else {
  75.                 s.append(c);
  76.             }
  77.         }
  78.  
  79.         return s.toString();
  80.     }
  81.  
  82.     public static String escapeQuote(String element)
  83.     {
  84.         String s = new String(element);    // don't change the original
  85.         String [] metas = {"\"", "'"};
  86.         String [] repls = {"\\\"", "\\'"};
  87.         for(int i = 0; i < metas.length; i++) {
  88.             int pos=0;
  89.             do {
  90.                 pos = s.indexOf(metas[i], pos);
  91.                 if(pos<0)
  92.                     break;
  93.  
  94.                 s = s.substring(0, pos) + repls[i] + s.substring(pos+1);
  95.                 pos++;
  96.             } while(pos >= 0);
  97.         }
  98.  
  99.         return s;
  100.     }
  101.  
  102.     public static boolean isNameChar(char c)
  103.     {
  104.         return true;
  105.     }
  106.  
  107.     public static boolean isNameChar(String s)
  108.     {
  109.         String alt_valid = "./+-_";
  110.  
  111.         for(int i=0; i<s.length(); i++)
  112.             if(!Character.isLetterOrDigit(s.charAt(i)) &&
  113.                     alt_valid.indexOf(s.charAt(i))<0)
  114.                 return false;
  115.         return true;
  116.     }
  117.  
  118.     public static void debug_print(String msg)
  119.     {
  120.         if(!debug)
  121.             return;
  122.  
  123.         System.err.println(msg);
  124.     }
  125.  
  126.     public static void debug_print(Object o)
  127.     {
  128.         debug_print(o.toString());
  129.     }
  130. }
  131.